/* BOXES */
function fn_buy_card() {
	$.ajaxRequest(index_script + 'index.php?r=transaction/buycard', {
		result_ids: 'buycard',
		callback: function(data, params) {
			var opts = {
				custom_class: 'dialog_buycard',
				buttons: false,
				width: 620
			}

			cm_dialog(lang.payment, data.html.buycard, opts)
			$('#tabs-buyacard ul a').live('click', function() {
				var jtabs = $(this).parents('#tabs-slideshow');

				jtabs.find('> ul a').removeClass('current');
				jtabs.find('div.tab').hide();

				$(this).addClass('current');
				jtabs.find($(this).attr('href')).show();
			});
		}
	});
}

function fn_transactions() {
	$.ajaxRequest(index_script + 'index.php?r=transaction', {
		result_ids: 'user_transactions',
		callback: function(data, params) {
			if ( data.login_required ) {
				fn_login_form();
				return ;
			}

			var opts = {
				custom_class: 'dialog_transactions',
				width: 850,
				buttons: [
					{caption: lang.ok}
				]
			}

			cm_dialog(lang.transactions, data.html.user_transactions, opts)
		}
	});
}

function fn_account() {
	$.ajaxRequest(index_script + 'index.php?r=user/balance', {
		result_ids: 'user_balance',
		callback: function(data, params) {
			if ( data.login_required ) {
				fn_login_form();
				return ;
			}

			var opts = {
				custom_class: 'dialog_account',
				buttons: [
					{caption: lang.buy_card, callback: fn_buy_card}
					, {caption: lang.prev_transactions, callback: fn_transactions}
					, {caption: lang.update_account, callback: function() {
						window.location = index_script + '?r=user/account';
					}}
				]
			}

			cm_dialog(data.user_name ? data.user_name : lang.balance, data.html.user_balance, opts)
		}
	});
}

function fn_login_form(close_last) {
	$.ajaxRequest(index_script + 'index.php?r=user/login&popup=1', {
		result_ids: 'login_form_popup',
		callback: function(data, params) {
			var opts = {
				custom_class: 'dialog_login',
				width: 500,
				buttons: false
			}

			if ( close_last ) cm_dialog_get_last().close();
			cm_dialog('Bạn cần đăng nhập trước khi thực hiện chức năng này', '<div id="login_form_popup">'+data.html.login_form_popup+'</div>', opts);
			jQuery.processForms($('.dialog_login').get());
		}
	});
}

function fn_register_form(close_last) {
	$.ajaxRequest(index_script + 'index.php?r=user/register', {
		result_ids: 'register_form',
		callback: function(data, params) {
			var opts = {
				custom_class: 'dialog_login',
				width: 500,
				buttons: false
			}

			if ( close_last ) cm_dialog_get_last().close();
			cm_dialog(lang.register, '<div id="register_form">'+data.html.register_form+'</div>', opts);
			jQuery.processForms($('.dialog_login').get());
		}
	});
}

function fn_add_to_cart(pid) {
	$.ajaxRequest(index_script + 'index.php?r=product/addToCart&pid='+pid, {
		result_ids: 'addtocart_result,mini_cart',
		callback: function(data, params) {
			if (data.addtocart_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					width: 700,
					buttons: [
						{caption: lang.place_order, callback: function() {
							fn_button_place_order();
						}}
						, {caption: lang.continuous_shopping}
					]
				}
			} else if (data.addtocart_ok == 2 ) {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.bookstore, callback: function() {
							window.location = index_script + 'trang-ca-nhan#user-books';
						}}
						, {caption: lang.cancel}
					]
				}
			}

			cm_dialog(caption, data.html.addtocart_result, opts)
		}
	});
}

function fn_button_place_order() {
	$.ajaxRequest(index_script + 'index.php?r=product/placeOrder', {
		result_ids: 'placeorder',
		callback: fn_place_order
	});
}

function fn_place_order(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.placeorder ) {
		if ( data.allow_place_order ) {
			var opts = {
				custom_class: 'dialog_placeorder',
				buttons: [
					{caption: lang.place_order, callback: function() {
						fn_do_place_order();
					}}
					, {caption: lang.cancel}
				]
			}
		} else {
			var opts = {
				custom_class: 'dialog_placeorder',
				buttons: [
					{caption: lang.cancel}
				]
			}
		}

		cm_dialog(lang.confirmation, data.html.placeorder, opts)
	}
}

function fn_do_place_order() {
	$.ajaxRequest(index_script + 'index.php?r=product/doPlaceOrder', {
		result_ids: 'placeorder_result,mini_cart',
		callback: function(data, params) {
			if (data.placeorder_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					buttons: [
						{caption: lang.bookstore, callback: function() {
								window.location = index_script + 'trang-ca-nhan#user-books';
						}}
						, {caption: lang.account, callback: fn_account}
						, {caption: lang.continuous_shopping}
					]
				}
			} else {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.account, callback: fn_account}
						, {caption: lang.cancel}
					]
				}
			}

			cm_dialog(caption, data.html.placeorder_result, opts)
		}
	});
}

function fn_do_buy_now(pid) {
	$.ajaxRequest(index_script + 'index.php?r=product/dobuy&pid='+pid, {
		result_ids: 'dobuy_result',
		callback: function(data, params) {
			if (data.buy_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					buttons: [
						{caption: lang.cancel}
						, 
						{caption: lang.bookstore, callback: function() {
								window.location = index_script + 'trang-ca-nhan#user-books';
						}}
					]
				}
			} else if (data.buy_ok == 2 ) {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.bookstore, callback: function() {
								window.location = index_script + 'trang-ca-nhan#user-books';
						}}
						, {caption: lang.cancel}
					]
				}
			} 
			else 
			{
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.account, callback: fn_account}
						, {caption: lang.cancel}
					]
				}
			}

			cm_dialog(caption, data.html.dobuy_result, opts)
		}
	});
}

function fn_buy_now(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.product_buy_now ) {
		var opts = {
			custom_class: 'dialog_buy_now',
			buttons: [
//				{caption: lang.add_to_cart, callback: function() {
//						fn_add_to_cart(data.product_id);
//				}, custom_class: 'btn-add-to-cart'}
//				, {caption: lang.buy_now, callback: function() {
//						fn_do_buy_now(data.product_id);
//				}, custom_class: 'btn-buy-now'}
//				, {caption: lang.cancel}
			]
		}

		cm_dialog(lang.confirmation, data.html.product_buy_now, opts)
	}
}

function fn_do_rent(pid, rent_days) {
	$.ajaxRequest(index_script + 'index.php?r=product/dorent&pid='+pid+'&days='+rent_days, {
		result_ids: 'dorent_result',
		callback: function(data, params) {
			if (data.rent_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					buttons: [
						{caption: lang.bookstore, callback: function() {
								window.location = index_script + 'trang-ca-nhan#user-renting';
						}}
					]
				}
			} else if (data.rent_ok == 2 ) {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.bookstore, callback: function() {
								window.location = index_script + 'trang-ca-nhan#user-renting';
						}}
						, {caption: lang.cancel}
					]
				}
			} else {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.account, callback: fn_account}
						, {caption: lang.cancel}
					]
				}
			}

			cm_dialog(caption, data.html.dorent_result, opts)
		}
	});
}

function fn_rent(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.product_rent ) {
		var opts = {
			custom_class: 'dialog_rent',
			buttons: [
				{caption: lang.rent, callback: function() {
					var rent_days = $(':checked[name=rent_days]').val();
					fn_do_rent(data.product_id, rent_days);
				}, custom_class: 'btn-rent'}
				, {caption: lang.cancel}
			]
		}

		cm_dialog(lang.confirmation, data.html.product_rent, opts)
	}
}
function fn_trailer(data, params) {
	if ( data.login_required ) 
	{
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.product_trailer ) 
	{
		var opts = {
			custom_class: 'dialog_trailer',
			buttons: 
			[]
		}
		cm_dialog("", data.html.product_trailer, opts)
	}
}
function fn_gift(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.product_gift ) {
		var opts = {
			custom_class: 'dialog_gift',
			buttons: [
				{caption: lang.gift, callback: function() {
					var user_id = $('#gift_user_id').val();
					if ( user_id > 0 ) {
						fn_do_gift(data.product_id, user_id);
					} else {
						alert('Xin vui lòng chọn người mà bạn muốn tặng sách!');
						return false;
					}
				}, custom_class: 'btn-gift'}
				, {caption: lang.cancel}
			]
		}

		cm_dialog(lang.confirmation, data.html.product_gift, opts)
	}
}

function fn_do_gift(pid, user_id) {
	$.ajaxRequest(index_script + 'index.php?r=product/dogift&pid='+pid+'&user_id='+user_id, {
		result_ids: 'dogift_result',
		callback: function(data, params) {
			if (data.gift_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					buttons: [
						{caption: data.friend_bookstore, callback: function() {
								window.location = index_script + data.friend_url + '#user-gift';
						}}
						, {caption: lang.continuous_shopping}
					]
				};
			} else if (data.gift_ok == 2 ) {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: data.friend_bookstore, callback: function() {
								console.log(index_script + data.friend_url + '#user-gift');
								window.location = index_script + data.friend_url + '#user-gift';
						}}
						, {caption: lang.cancel}
					]
				};
			} else {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.account, callback: fn_account}
						, {caption: lang.cancel}
					]
				};
			}

			cm_dialog(caption, data.html.dogift_result, opts)
		}
	});
}

function fn_lend(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}
	if ( data.html && data.html.product_lend ) {
		var opts = {
			custom_class: 'dialog_lend',
			buttons: [
				{caption: lang.lend, callback: function() {
					var user_id = $('#lend_user_id').val();
					if ( user_id > 0 ) {
						fn_do_lend(data.product_id, user_id);
					} else {
						alert('Xin vui lòng chọn người mà bạn muốn cho mượn sách!');
						return false;
					}
				}, custom_class: 'btn-lend'}
				, {caption: lang.cancel}
			]
		}

		cm_dialog(lang.confirmation, data.html.product_gift, opts)
	}
}

function fn_do_lend(pid, user_id) {
	$.ajaxRequest(index_script + 'index.php?r=product/dolend&pid='+pid+'&user_id='+user_id, {
		result_ids: 'dolend_result',
		callback: function(data, params) {
			if (data.lend_ok == 1 ) {
				var caption = lang.success;
				var opts = {
					buttons: [
						{caption: data.friend_bookstore, callback: function() {
								window.location = index_script + data.friend_url + '#user-book';
						}}
						, {caption: lang.continuous_shopping}
					]
				};
			} else if (data.lend_ok == 2 ) {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: data.friend_bookstore, callback: function() {
								console.log(index_script + data.friend_url + '#user-book');
								window.location = index_script + data.friend_url + '#user-book';
						}}
						, {caption: lang.cancel}
					]
				};
			} else {
				var caption = lang.error;
				var opts = {
					buttons: [
						{caption: lang.account, callback: fn_account}
						, {caption: lang.cancel}
					]
				};
			}

			cm_dialog(caption, data.html.dolend_result, opts)
		}
	});
}

function fn_product_sach_bo(data, params) {
	if ( data.login_required ) {
		fn_login_form();
		return ;
	}

	var result_id = params.result_ids;
	if ( data.html && data.html[result_id] ) {
		var opts = {
			width: 760,
			height: 400,
			custom_class: 'dialog_sachbo',
			buttons: [
				{caption: lang.ok}
			]
		}

		cm_dialog(params.obj.attr('title'), data.html[result_id], opts);
		js_items_slider();
	}
}